// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './common/index.js'
import CommonUtil from './common/commonUtil.js'

// 解决移动端点击事件300ms延迟问题
import fastClick from 'fastclick'
import Toast from './common/toast'
import axios from 'axios'
import store from './store'
import http from './http'
import VueAwesomeSwiper from 'vue-awesome-swiper'
// 解决不同浏览器默认样式不同的问题
import 'style/reset.css'
// 解决移动端1PX像素的问题
import 'style/border.css'
import 'style/iconfont.css'
import 'style/animate.css'
import 'style/common.css'
import 'swiper/dist/css/swiper.css'
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import _ from 'lodash'
import VueClipboard from 'vue-clipboard2'

Vue.config.productionTip = false
fastClick.attach(document.body)
Vue.use(Toast)
Vue.use(MintUI)
Vue.use(CommonUtil)
Vue.use(VueAwesomeSwiper)
Vue.use(VueClipboard)
Vue.prototype.axios = axios
Vue.filter('formatUnit', function (value) {
  if (!value) return
  if (value % 10000 === 0) {
    return value / 10000 + '万'
  }
  return value
})
Vue.filter('formatTimeUnit', function (value, fmt) {
  if (!value) return
  if (fmt == 'false' && value == 'month') {
    return '月'
  }
  switch (value) {
    case 'year':
      return '年'
    case 'month':
      return '个月'
    case 'day':
      return '天'
  }
})
Vue.filter('dataFormat', function (value, fmt) {
  if(!value) return
  let getDate = new Date(value);
  let o = {
    'M+': getDate.getMonth() + 1,
    'd+': getDate.getDate(),
    'h+': getDate.getHours(),
    'm+': getDate.getMinutes(),
    's+': getDate.getSeconds(),
    'q+': Math.floor((getDate.getMonth() + 3) / 3),
    'S': getDate.getMilliseconds()
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt;
})
//前四后二
Vue.filter('fieldEllipsis',function (value) {
  if (!value) return '-'
  if (value.length<7) return value
  var length = value.length
  return value.substring(0,3)+'****'+value.substring(length-4)
})
//保留四个字
Vue.filter('fieldFour',function (value) {
  if (!value) return '-'
  if (value.length<5) return value
  var length = value.length
  return value.substring(0,4)
})
Vue.filter('nameEllipsis',function (value) {
  if (!value) return '-';
  return '*'+value.substring(1)
})


Vue.directive('title', {
  inserted: function (el, binding) {
    document.title = el.dataset.title
  }
})
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  if (!to.query.token && to.matched.some(res => res.meta.requireToken)) {// 判断是否需要登录权限
    if (localStorage.getItem('token')) {// 判断是否登录
      next()
    } else {// 没登录则跳转到登录界面
      next({
        path: '/login'
      })
    }
  } else {
    next()
  }
})

Vue.prototype._ = _;
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  _,
  components: {App},
  template: '<App/>'
})
